#!/bin/bash
# 设置默认值
DEFAULT_BRANCH="sm8650"
DEFAULT_MANIFEST="oneplus12_v"

# 获取用户输入
read -p "请输入分支名称 [默认: $DEFAULT_BRANCH]: " branch
read -p "请输入 manifest 文件名 [默认: $DEFAULT_MANIFEST]: " manifest

# 使用默认值如果用户没有输入
branch=${branch:-$DEFAULT_BRANCH}
manifest=${manifest:-$DEFAULT_MANIFEST}

# 执行 repo init 命令
echo "正在执行 repo init..."
echo "分支: $branch"
echo "manifest 文件: $manifest"

echo "正在创建并进入 kernel_workspace 目录..."
mkdir kernel_workspace && cd kernel_workspace || { echo "创建或进入目录失败！"; exit 1; }

echo "正在初始化 repo 仓库..."
echo "使用分支: $branch"
echo "使用 manifest 文件: $manifest"
repo init -u https://github.com/Suxiaoqinx/Suxiaoqinx -m gtneo6.xml --depth=1 || { echo "repo 初始化失败！"; exit 1; }

echo "正在同步代码仓库..."
echo "使用并行任务数: $(nproc --all)"
repo sync -c -j$(nproc --all) --no-tags --no-clone-bundle --force-sync || { echo "代码同步失败！"; exit 1; }

echo "正在清理受保护的导出文件..."
rm kernel_platform/common/android/abi_gki_protected_exports_* || echo "未找到 common/android 下的受保护导出文件"
rm kernel_platform/msm-kernel/android/abi_gki_protected_exports_* || echo "未找到 msm-kernel/android 下的受保护导出文件"

echo "正在修改 setlocalversion 脚本移除 -dirty 标记..."
sed -i 's/ -dirty//g' kernel_platform/common/scripts/setlocalversion || echo "修改 common/setlocalversion 失败"
sed -i 's/ -dirty//g' kernel_platform/msm-kernel/scripts/setlocalversion || echo "修改 msm-kernel/setlocalversion 失败"
sed -i 's/ -dirty//g' kernel_platform/external/dtc/scripts/setlocalversion || echo "修改 external/dtc/setlocalversion 失败"

echo "正在向 setlocalversion 添加额外的清理逻辑..."
sed -i '$i res=$(echo "$res" | sed '\''s/-dirty//g'\'')' kernel_platform/common/scripts/setlocalversion || echo "添加 common 清理逻辑失败"
sed -i '$i res=$(echo "$res" | sed '\''s/-dirty//g'\'')' kernel_platform/msm-kernel/scripts/setlocalversion || echo "添加 msm-kernel 清理逻辑失败"
sed -i '$i res=$(echo "$res" | sed '\''s/-dirty//g'\'')' kernel_platform/external/dtc/scripts/setlocalversion || echo "添加 dtc 清理逻辑失败"

echo "正在自定义版本号格式..."
sed -i '$s|echo "\$res"|echo "-android14-11-o-g6567d1f5c5e2"|' kernel_platform/common/scripts/setlocalversion || echo "修改 common 版本号失败"
sed -i '$s|echo "\$res"|echo "-android14-11-o-g6567d1f5c5e2"|' kernel_platform/msm-kernel/scripts/setlocalversion || echo "修改 msm-kernel 版本号失败"
sed -i '$s|echo "\$res"|echo "-android14-11-o-g6567d1f5c5e2"|' kernel_platform/external/dtc/scripts/setlocalversion || echo "修改 dtc 版本号失败"

echo "代码仓库同步和预处理完成！"

# Apply SUSFS patches
# 进入 kernel_platform 目录应用 SUSFS 补丁
echo "正在进入 kernel_platform 目录应用 SUSFS 补丁..."

# Navigate to the kernel workspace
cd kernel_platform

# 下载并执行 setup 脚本
curl -LSs "https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU-Ultra/main/kernel/setup.sh" | bash -s susfs-main

# 进入 KernelSU 目录
cd ./KernelSU

# 尝试获取 KSU_API_VERSION 从 Makefile (重试 3 次)
for i in {1..3}; do
  KSU_API_VERSION=$(curl -s "https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU-Ultra/susfs-main/kernel/Makefile" | \
    grep -m1 "KSU_VERSION_API :=" | \
    awk -F'= ' '{print $2}' | \
    tr -d '[:space:]')
  if [ -n "$KSU_API_VERSION" ]; then
    break
  else
    sleep 2
  fi
done

# 如果 KSU_API_VERSION 为空，则设置一个默认值
if [ -z "$KSU_API_VERSION" ]; then
  KSU_API_VERSION="3.1.7"
fi

# 定义 Makefile 中的版本信息 (包含括号中的 KSU_VERSION)
VERSION_DEFINITIONS=$(cat << EOF
define get_ksu_version_full
v\\$1-SukiSU-Suxiaoqing
endef

KSU_VERSION_API := $KSU_API_VERSION
KSU_VERSION_FULL := v$KSU_API_VERSION-SukiSU-Suxiaoqing
EOF
)

# 从 Makefile 中删除旧的 KSU 版本定义并添加新的
sed -i '/define get_ksu_version_full/,/endef/d' kernel/Makefile
sed -i '/KSU_VERSION_API :=/d' kernel/Makefile
sed -i '/KSU_VERSION_FULL :=/d' kernel/Makefile

# 将新的版本信息插入到 Makefile 中
awk -v def="$VERSION_DEFINITIONS" '
  /REPO_OWNER :=/ {print; print def; inserted=1; next}
  1
  END {if (!inserted) print def}
' kernel/Makefile > kernel/Makefile.tmp && mv kernel/Makefile.tmp kernel/Makefile

# 输出 KSU 版本信息
#grep -A10 "REPO_OWNER" kernel/Makefile
#grep "KSU_VERSION_FULL" kernel/Makefile

# 补丁应用成功
echo "SukiSU 补丁应用成功完成！"

cd ~/kernel_workspace
# Create common directory for SUSFS patches
echo "正在克隆 SUSFS 补丁仓库..."
git clone https://gitlab.com/simonpunk/susfs4ksu.git -b gki-android14-6.1
echo "正在克隆 kernel_patches 仓库..."
git clone https://github.com/Xiaomichael/kernel_patches.git
echo "正在克隆 SukiSU_patch 仓库..."
git clone https://github.com/ShirkNeko/SukiSU_patch.git

# Copy SUSFS patches
echo "正在复制 SUSFS 补丁文件..."
cd kernel_platform
echo "复制 50_add_susfs_in_gki-android14-6.1.patch..."
cp ../susfs4ksu/kernel_patches/50_add_susfs_in_gki-android14-6.1.patch ./common/
echo "复制 syscall_hooks.patch..."
cp ../kernel_patches/next/syscall_hooks.patch ./common/
echo "复制 fs/ 目录下的补丁..."
cp ../susfs4ksu/kernel_patches/fs/* ./common/fs/
echo "复制 include/linux/ 目录下的补丁..."
cp ../susfs4ksu/kernel_patches/include/linux/* ./common/include/linux/

#echo "复制 zram_atgc..."
#cp -r ../zram_atgc/* ./common/drivers/block/zram

echo "进入 common 目录应用补丁..."
cd ./common
echo "正在应用 50_add_susfs_in_gki-android14-6.1.patch..."
patch -p1 < 50_add_susfs_in_gki-android14-6.1.patch || echo "应用 50_add_susfs_in_gki-android14-6.1.patch 失败或已应用"

echo "正在复制并应用 69_hide_stuff.patch..."
cp ../../kernel_patches/69_hide_stuff.patch ./
patch -p1 -F 3 < 69_hide_stuff.patch || echo "应用 69_hide_stuff.patch 失败或已应用"

echo "正在应用 syscall_hooks.patch..."
patch -p1 -F 3 < syscall_hooks.patch || echo "应用 syscall_hooks.patch 失败或已应用"

#echo "正在应用 001-lz4.patch..."
#git apply -p1 < 001-lz4.patch || echo "应用 001-lz4.patch 失败或已应用"

#echo "正在复制并应用 lz4kd.patch..."
#cp ../../SukiSU_patch/other/zram/zram_patch/6.1/lz4kd.patch ./
#patch -p1 -F 3 < lz4kd.patch || echo "应用 lz4kd.patch 失败或已应用"

#echo "正在复制并应用 lz4k_oplus.patch..."
#cp ../../SukiSU_patch/other/zram/zram_patch/6.1/lz4k_oplus.patch ./
#patch -p1 -F 3 < lz4k_oplus.patch || echo "应用 lz4k_oplus.patch 失败或已应用"

echo "正在下载并应用 zram_atgc.patch..."
curl -fsLO https://gitee.com/GuXuandadaxxx/suxiaoq/raw/master/zram_atgc.patch
patch -p1 -N < zram_atgc.patch || echo "应用 zram_atgc.patch 失败或已应用"

echo "正在下载并应用 llvmo3.patch..."
curl -fsLO https://gitee.com/GuXuandadaxxx/suxiaoq/raw/master/llvmo3.patch
patch -p1 < llvmo3.patch || echo "应用 llvmo3.patch 失败或已应用"

echo "所有补丁应用完成！"

cd ~/kernel_workspace/kernel_platform
# 定义配置文件路径
CONFIG_FILE="./common/arch/arm64/configs/gki_defconfig"
BUILD_CONFIG="./common/build.config.gki"

# 检查文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
    echo "错误: $CONFIG_FILE 不存在！"
    exit 1
fi

if [ ! -f "$BUILD_CONFIG" ]; then
    echo "错误: $BUILD_CONFIG 不存在！"
    exit 1
fi

# 添加 KSU 配置
echo "添加 KSU 配置..."
echo "CONFIG_KSU=y" >> "$CONFIG_FILE"
echo "CONFIG_KPM=y" >> "$CONFIG_FILE"
echo "CONFIG_LLVM_POLLY=y" >> "$CONFIG_FILE"
echo "CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE_O3=y" >> "$CONFIG_FILE"
echo "CONFIG_HAVE_O3=y" >> "$CONFIG_FILE"

echo "CONFIG_KSU_SUSFS_SUS_SU=n" >> "$CONFIG_FILE"
echo "CONFIG_KSU_MANUAL_HOOK=y" >> "$CONFIG_FILE"

# 添加 SUSFS 配置
echo "添加 SUSFS 配置..."
echo "CONFIG_KSU_SUSFS=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_HAS_MAGIC_MOUNT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_SUS_PATH=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_SUS_MOUNT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_AUTO_ADD_SUS_KSU_DEFAULT_MOUNT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_SUS_KSTAT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_SUS_OVERLAYFS=n" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_TRY_UMOUNT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_AUTO_ADD_TRY_UMOUNT_FOR_BIND_MOUNT=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_SPOOF_UNAME=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_ENABLE_LOG=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_HIDE_KSU_SUSFS_SYMBOLS=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_SPOOF_CMDLINE_OR_BOOTCONFIG=y" >> "$CONFIG_FILE"
echo "CONFIG_KSU_SUSFS_OPEN_REDIRECT=y" >> "$CONFIG_FILE"

# 添加 BBR v1
echo "添加 BBR 配置（v1）..."
echo "CONFIG_TCP_CONG_ADVANCED=y" >> "$CONFIG_FILE"
echo "CONFIG_TCP_CONG_BBR=y" >> "$CONFIG_FILE"
echo "CONFIG_TCP_CONG_BRUTAL=y" >> "$CONFIG_FILE"
echo "CONFIG_NET_SCH_FQ=y" >> "$CONFIG_FILE"

# 禁用其他不用的算法（可选）
echo "CONFIG_TCP_CONG_BIC=n" >> "$CONFIG_FILE"
echo "CONFIG_TCP_CONG_WESTWOOD=n" >> "$CONFIG_FILE"
echo "CONFIG_TCP_CONG_HTCP=n" >> "$CONFIG_FILE"

# 添加 lz4kd 配置
echo "添加 lz4kd 配置..."
echo "CONFIG_CRYPTO_LZ4KD=y" >> "$CONFIG_FILE"
echo "CONFIG_CRYPTO_LZ4K_OPLUS=y" >> "$CONFIG_FILE"
echo "CONFIG_ZRAM_WRITEBACK=y" >> "$CONFIG_FILE"

# 添加 ATGC 配置
echo "CONFIG_ZRAM_ATGC=y" >> "$CONFIG_FILE"

# 移除 check_defconfig
echo "移除 check_defconfig..."
sed -i 's/check_defconfig//' "$BUILD_CONFIG"

echo "添加成功~"

cd ~/kernel_workspace
# 下载并设置 Clang 21
echo "正在下载 Clang 21..."
mkdir -p clang-21 && cd clang-21
wget https://api.fate.vip/https://github.com/ZyCromerZ/Clang/releases/download/21.0.0git-20250618-release/Clang-21.0.0git-20250618.tar.gz
echo "Clang 21.0.0 下载完成，正在解压..."
if [ $? -ne 0 ]; then
    echo "下载失败，请检查网络连接或链接是否有效。" >&2
    exit 1
fi
tar -xzf Clang-21.0.0git-20250618.tar.gz
if [ $? -ne 0 ]; then
    echo "解压失败，请检查下载的文件是否完整。" >&2
    exit 1
fi
rm Clang-21.0.0git-20250618.tar.gz
echo "Clang 21.0.0 已下载并解压到 clang-21 目录中。"